package com.neusoft.neps.controller;

import com.neusoft.neps.method.MissionIO;
import com.neusoft.grid.entity.Mission;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class SelectAqiController {

    @FXML
    private Button level1Button;
    @FXML
    private Button level2Button;
    @FXML
    private Button level3Button;
    @FXML
    private Button level4Button;
    @FXML
    private Button level5Button;
    @FXML
    private Button level6Button;
    @FXML
    private TextArea feedbackField;
    @FXML
    private Button submitButton;
    @FXML
    private Button backButton;

    private int selectedLevel = 0;
    private String AqiLevel;

    @FXML
    private void initialize() {
        selectLevel(1);
    }

    @FXML
    private void handleLevel1Select() {
        selectLevel(1);
        AqiLevel = "一级";
    }

    @FXML
    private void handleLevel2Select() {
        selectLevel(2);
        AqiLevel = "二级";
    }

    @FXML
    private void handleLevel3Select() {
        selectLevel(3);
        AqiLevel = "三级";
    }

    @FXML
    private void handleLevel4Select() {
        selectLevel(4);
        AqiLevel = "四级";
    }

    @FXML
    private void handleLevel5Select() {
        selectLevel(5);
        AqiLevel = "五级";
    }

    @FXML
    private void handleLevel6Select() {
        selectLevel(6);
        AqiLevel = "六级";
    }

    @FXML
    private void handleSubmit() throws IOException {
        if (feedbackField.getText().isEmpty()) {
            showAlert("信息不完整", "请填写反馈信息");
            return;
        }
        putin();
        // 创建反馈对象 (这里简化处理，实际应用中需要从上一页面获取位置信息
        showAlert("提交成功", "您的空气质量反馈已成功提交！");

        try {
            navigateTo("../view/feedbackListView.fxml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void putin() throws IOException {
        List<Mission> missions = new ArrayList<>();
        try {
            missions = MissionIO.readMissions("missions.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<Mission> nowMission = MissionIO.readMissions("nowMission.txt");
        Mission newMission = nowMission.getFirst();
        newMission.setLevel(AqiLevel);
        newMission.setDescription(feedbackField.getText());
        missions.add(newMission);
        System.out.println(missions.size());
        try {
            MissionIO.writeMissions(missions, "missions.txt");
            System.out.println("missions.txt写入成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @FXML
    private void handleBack() {
        try {
            navigateTo("../view/SelectGridView.fxml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void selectLevel(int level) {
        resetButtonStyles();
        selectedLevel = level;

        switch (level) {
            case 1:
                level1Button
                        .setStyle("-fx-background-color: #02E300; -fx-border-color: #000000; -fx-border-width: 2px;");
                break;
            case 2:
                level2Button
                        .setStyle("-fx-background-color: #FFFF00; -fx-border-color: #000000; -fx-border-width: 2px;");
                break;
            case 3:
                level3Button
                        .setStyle("-fx-background-color: #FF7E00; -fx-border-color: #000000; -fx-border-width: 2px;");
                break;
            case 4:
                level4Button
                        .setStyle("-fx-background-color: #FE0000; -fx-border-color: #000000; -fx-border-width: 2px;");
                break;
            case 5:
                level5Button
                        .setStyle("-fx-background-color: #98004B; -fx-border-color: #000000; -fx-border-width: 2px;");
                break;
            case 6:
                level6Button
                        .setStyle("-fx-background-color: #7E0123; -fx-border-color: #000000; -fx-border-width: 2px;");
                break;
        }
    }

    private void resetButtonStyles() {
        level1Button.setStyle("-fx-background-color: #02E300;");
        level2Button.setStyle("-fx-background-color: #FFFF00;");
        level3Button.setStyle("-fx-background-color: #FF7E00;");
        level4Button.setStyle("-fx-background-color: #FE0000;");
        level5Button.setStyle("-fx-background-color: #98004B;");
        level6Button.setStyle("-fx-background-color: #7E0123;");
    }

    private void navigateTo(String fxmlPath) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader();
        Stage stage = (Stage) submitButton.getScene().getWindow();
        URL url = getClass().getResource(fxmlPath);
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root, 600, 400);
        stage.setScene(scene);
        stage.show();
    }

    private void showAlert(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
}